<template>
	<view class="my">
		<view class="my-head">
			<userinfo
				:isLogin="!!token" 
				:avatar="userInfo.avatar" 
				:nickname="userInfo.nickname" 
				:phone="userInfo.mobile" 
				:storeName="userInfo.storeName" 
				:postName="PostTypeLabel[userInfo.postId]" 
				:isSuper="userInfo.superFlag == SuperFlag" 
				@login="toLoginPage" 
				@userinfoclick="toUserinfoPage" 
				@bindphone="toBindPhonePage"
			></userinfo>
		</view>
		
		<view class="my-body">
			<view class="menu-wrap">
				<!-- 总店超管或者分店超管 -->
				<template v-if="userInfo.superFlag == SuperFlag">
					<menu-cell
						:icon="require('../../static/icons/menu-people.png')" 
						label="员工管理" 
						path="/pages/staff/index" 
						@click="toMenuPage"
					></menu-cell>
					<u-line color="#eee"></u-line>
				</template>
				<!-- 总店超管 -->
				<template v-if="userInfo.superFlag == SuperFlag && userInfo.storeType == StoreType.main">
					<menu-cell
						:icon="require('../../static/icons/menu-home.png')" 
						label="分店管理" 
						path="/pages/store/index" 
						@click="toMenuPage"
					></menu-cell>
					<u-line color="#eee"></u-line>
				</template>
				<!-- 总店超管或者总店库管 -->
				<template v-if="userInfo.storeType == StoreType.main && (userInfo.superFlag == SuperFlag || userInfo.postId == PostType.stock)">
					<menu-cell
						:icon="require('../../static/icons/menu-form.png')" 
						label="录入库存" 
						path="/pages/stock/create"
						@click="toMenuPage"
					></menu-cell>
					<u-line color="#eee"></u-line>
				</template>
				<!-- 超管或者任何岗位 -->
				<template v-if="userInfo.superFlag == SuperFlag || userInfo.postId">
					<menu-cell
						:icon="require('../../static/icons/menu-scan.png')" 
						label="扫码操作" 
						@click="onToggleScanType"
					></menu-cell>
				</template>
			</view>
		</view>
		
		<u-modal 
			title="扫码操作" 
			:show="showScanType" 
			width="580rpx" 
			showCancelButton 
			confirmText="确认扫码" 
			@confirm="onScanConfirm" 
			@cancel="onToggleScanType" 
		>
			<u-radio-group
				v-model="currentScanType" 
				shape="square" 
				placement="column"
				iconPlacement="right" 
				activeColor="#0E94FC">
				<view class="m-t-40" v-for="item in scanTypeOptions" :key="item.value">
					<u-radio
						:label="item.name" 
						:name="item.value" 
						size="36rpx"
						iconSize="24rpx" 
						labelSize="28rpx" 
						:labelColor="item.value == currentScanType ? '#0E94FC' : '#666'" 
					></u-radio>
				</view>
			</u-radio-group>
		</u-modal>
	</view>
</template>

<script>
	import userMixins from '@/mixins/user.js'
	import userinfo from './components/userinfo.vue'
	import menuCell from './components/menu-cell.vue'
	import { ScanType, PostType, PostTypeLabel, StoreType, SuperFlag } from '@/utils/enum.js'
	import { scanChangeStock } from '@/api/stock.js'
	
	export default {
		mixins: [userMixins],
		components: { userinfo, menuCell },
		data() {
			return {
				showScanType: false,
				scanTypeOptions: [],
				currentScanType: null,
				
				PostType,
				PostTypeLabel,
				StoreType,
				SuperFlag,
			}
		},
		async onShow() {
			this.$wxSdkUtil.init(null, {
				jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'showMenuItems', 'hideMenuItems', 'scanQRCode'],
				hideShare: true,
			})
			
			if (this.token) {
				await this.getUserInfo()
				this.generateScanOptions()
			}
		},
		methods: {
			// 生成扫码类型集合
			generateScanOptions() {
				let scanTypeOptions = []
				// 总店超管
				if (this.userInfo.superFlag == SuperFlag && this.userInfo.storeType == StoreType.main) {
					scanTypeOptions = [
						{ name: '入库', value: ScanType.addStock },
						{ name: '出库', value: ScanType.outStock },
						{ name: '作废', value: ScanType.cancel },
						{ name: '门店销售', value: ScanType.storeSale },
						{ name: '直播销售', value: ScanType.liveSale },
						{ name: '微信销售', value: ScanType.wechatSale },
					]
				}
				// 分店超管
				else if (this.userInfo.superFlag == SuperFlag && this.userInfo.storeType == StoreType.branch) {
					scanTypeOptions = [
						{ name: '入库', value: ScanType.addStock },
						{ name: '门店销售', value: ScanType.storeSale },
						{ name: '直播销售', value: ScanType.liveSale },
						{ name: '微信销售', value: ScanType.wechatSale },
					]
				}
				// 总店库管
				else if (this.userInfo.storeType == StoreType.main && this.userInfo.postId == PostType.stock) {
					scanTypeOptions = [
						{ name: '入库', value: ScanType.addStock },
						{ name: '出库', value: ScanType.outStock },
						{ name: '作废', value: ScanType.cancel },
					]
				}
				// 分店库管
				else if (this.userInfo.storeType == StoreType.branch && this.userInfo.postId == PostType.stock) {
					scanTypeOptions = [
						{ name: '入库', value: ScanType.addStock },
					]
				}
				// 员工
				else if (this.userInfo.postId == PostType.staff) {
					scanTypeOptions = [
						{ name: '门店销售', value: ScanType.storeSale },
						{ name: '直播销售', value: ScanType.liveSale },
						{ name: '微信销售', value: ScanType.wechatSale },
					]
				}
				this.scanTypeOptions = scanTypeOptions
			},
			// 去登陆页
			toLoginPage() {
				this.token ? this.toUserinfoPage() : uni.reLaunch({
					url: '/pages/login/index'
				})
			},
			// 去绑定手机号页
			toBindPhonePage() {
				uni.navigateTo({
					url: '/pages/bind-phone/index'
				}, true)
			},
			// 去个人信息页
			toUserinfoPage() {
				uni.navigateTo({
					url: '/pages/userinfo/index'
				}, true)
			},
			// 菜单点击
			toMenuPage(path) {
				uni.navigateTo({
					url: path
				}, true)
			},
			// 显示扫码类型
			onToggleScanType() {
				this.showScanType = !this.showScanType
			},
			// 确认扫码
			async onScanConfirm() {
				if (!this.currentScanType) {
					return uni.showToast({
						title: '请选择扫码类型',
						icon: 'none'
					})
				}
				if (!this.$wxSdkUtil.isReady) {
					return uni.showToast({
						title: '等待SDK完成初始化',
						icon: 'none'
					})
				}
				this.$wxSdkUtil.wx.scanQRCode({
					needResult: 1,
					scanType: ['qrCode', 'barCode'],
					success: async (res) => {
						console.log('扫码返回', res)
						try {
							uni.showLoading({ title: '正在操作', mask: true })
							await scanChangeStock({
								type: this.currentScanType, 
								identityCode: res.resultStr,
							})
							const scanTypeInfo = this.scanTypeOptions.find(v => v.value == this.currentScanType)
							uni.showToast({
								title: `${scanTypeInfo ? scanTypeInfo.name : '操作'}成功`,
								icon: 'success'
							})
						} catch(e) {
							uni.showToast({
								title: e.message,
								icon: 'none'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my {
		&-head {
			height: 400rpx;
			padding-top: 30rpx;
			box-sizing: border-box;
			background: linear-gradient(to bottom, #effaff, $u-bg-color);
		}
		&-body {
			margin-top: -200rpx;
			padding: 30rpx;
			.menu-wrap {
				padding: 10rpx 30rpx;
				background-color: #fff;
				border-radius: 16rpx;
			}
		}
	}
</style>